package com.twu.biblioteca.services;

import com.twu.biblioteca.Constants.Movies;
import com.twu.biblioteca.Constants.Users;
import com.twu.biblioteca.elements.Movie;
import com.twu.biblioteca.elements.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovieLogicService {
    private final Movies movies;
    private final Users users;

    public MovieLogicService(Movies movies, Users users) {
        this.movies = movies;
        this.users = users;
    }

    public List<Movie> getAllAvailableMovies() {
        List<Movie> allMovies = movies.getAllMovies();

        List<Movie> allAvailableMovies = new ArrayList<Movie>();
        for(Movie movie : allMovies) {
            if (movie.isAvailable()) {
                allAvailableMovies.add(movie);
            }
        }

        return allAvailableMovies;
    }

    public boolean login(String userId, String password) {
        List<User> allUsers = users.getAllUserInformation();
        for (User user : allUsers) {
            if (user.getUserId().equals(userId) && user.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }

    public boolean checkout(String movieName, User user) {
        Movie movie = getMovieByMovieName(movieName);

        if (movie == null) {
            return false;
        }

        movie.setOwner(user);
        return true;
    }

    private Movie getMovieByMovieName(String movieName) {
        for(Movie movie : getAllAvailableMovies()) {
            if (movie.getName().equals(movieName)) {
                return movie;
            }
        }
        return null;
    }

    public User getUserByUserId(String userId) {
        List<User> allUsers = users.getAllUserInformation();
        for(User user : allUsers) {
           if (user.getUserId().equals(userId)) {
               return user;
           }
        }
        return null;
    }
}
